package net.lico;

import org.apache.commons.lang.StringUtils;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

public class DictFileUtil {
    public static void main(String[] args) {
        String a = "a";
        String[] split = a.split(",");
        System.out.println(split.length);
        for (String s : split) {
            System.out.println(s);
        }

    }
    public static List<String> readDictFile(String file) throws Exception {
        List<String> dictContentList = new ArrayList<String>();
        BufferedReader br = null;
        try {
            Reader reader = new FileReader(file);
            br = new BufferedReader(reader);
            String line = null;
            while ((line = br.readLine()) != null) {
                if (StringUtils.isBlank(line)) continue;
//                if (!line.contains(",") || !line.contains("=")) continue;
                dictContentList.add(line.trim());
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (br != null) br.close();
        }
        br.close();
        return dictContentList;
    }

    public static void writeDictFile(String file, List<String> lineList) throws Exception {
        BufferedWriter bw = null;
        try {
            FileWriter wr = new FileWriter(file);
            bw = new BufferedWriter(wr);
            boolean isFirstLine = true;
            for (String line : lineList) {
                if (!isFirstLine) {
                    bw.newLine();
                } else {
                    isFirstLine = false;
                }
                bw.write(line);
            }
            bw.close();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (bw != null) bw.close();
        }

    }
}
